﻿using SNS.SnsSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Demo_DrawShape
{
    public partial class Frm_DrawShape : Form
    {
        object objShape;

        public Frm_DrawShape()
        {
            InitializeComponent();
            snsPictureBox1.ShapeEventHandler += SnsPictureBox1_ShapeEventHandler;
        }

        private void Frm_DrawShape_Load(object sender, EventArgs e)
        {
            Bitmap bitmap = (Bitmap)Image.FromFile(@"nature.jpg");
            snsPictureBox1.Image = bitmap;
        }

        private void SnsPictureBox1_ShapeEventHandler(object objShape)
        {
            this.objShape = objShape;
            snsPictureBox1.Refresh();
        }

        bool isDrawShapeWait = false;
        private void rbtnRespon_CheckedChanged(object sender, EventArgs e)
        {
            this.isDrawShapeWait = !rbtnRespon.Checked;
        }

        private void rbtnWait_CheckedChanged(object sender, EventArgs e)
        {
            this.isDrawShapeWait = rbtnWait.Checked;
        }

        private void btnDrawPoint_Click(object sender, EventArgs e)
        {
            this.objShape = new PointF();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawPointWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawPoint();
            }
        }

        private void btnDrawAdjPoint_Click(object sender, EventArgs e)
        {
            this.objShape = new PointF();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawAdjPointWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawAdjPoint();
            }
        }

        private void btnDrawLine_Click(object sender, EventArgs e)
        {
            this.objShape = new Line();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawLineWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawLine();
            }
        }

        private void btnDrawAdjLine_Click(object sender, EventArgs e)
        {
            this.objShape = new Line();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawAdjLineWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawAdjLine();
            }
        }

        private void btnDrawRect_Click(object sender, EventArgs e)
        {
            this.objShape = new Rectangle();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawRectWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawRect();
            }
        }

        private void btnDrawAdjRect_Click(object sender, EventArgs e)
        {
            this.objShape = new Rectangle();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawAdjRectWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawAdjRect();
            }
        }

        private void btnDrawCircle_Click(object sender, EventArgs e)
        {
            this.objShape = new Circle();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawCircleWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawCircle();
            }
        }

        private void btnDrawAdjCircle_Click(object sender, EventArgs e)
        {
            this.objShape = new Circle();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawAdjCircleWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawAdjCircle();
            }
        }

        private void btnDrawPolyline_Click(object sender, EventArgs e)
        {
            this.objShape = new Polyline();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawPolylineWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawPolyline();
            }
        }

        private void btnDrawAdjPolyline_Click(object sender, EventArgs e)
        {
            this.objShape = new Polyline();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawAdjPolylineWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawAdjPolyline();
            }
        }

        private void btnDrawPolygon_Click(object sender, EventArgs e)
        {
            this.objShape = new Polygon();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawPolygonWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawPolygon();
            }
        }

        private void btnDrawAdjPolygon_Click(object sender, EventArgs e)
        {
            this.objShape = new Polygon();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawAdjPolygonWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawAdjPolygon();
            }
        }

        private void btnDrawFillRect_Click(object sender, EventArgs e)
        {
            this.objShape = new FillRectangle();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawFillRectWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawFillRect();
            }
        }

        private void btnDrawAdjFillRect_Click(object sender, EventArgs e)
        {
            this.objShape = new FillRectangle();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawAdjFillRectWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawAdjFillRect();
            }
        }

        private void btnDrawFillCircle_Click(object sender, EventArgs e)
        {
            this.objShape = new FillCircle();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawFillCircleWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawFillCircle();
            }
        }

        private void btnDrawAdjFillCircle_Click(object sender, EventArgs e)
        {
            this.objShape = new FillCircle();
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawAdjFillCircleWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawAdjFillCircle();
            }
        }

        private void btnDrawFillPolygon_Click(object sender, EventArgs e)
        {
            this.objShape = null;
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawFillPolygonWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawFillPolygon();
            }
        }

        private void btnDrawAdjFillPolygon_Click(object sender, EventArgs e)
        {
            this.objShape = null;
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawAdjFillPolygonWait();
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawAdjFillPolygon();
            }
        }

        private void btnDrawMeasLine_Click(object sender, EventArgs e)
        {
            this.objShape = null;
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawMeasLineWait(false, 0.1f);
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawMeasLine(false, 0.1f);
            }
        }

        private void btnDrawAdjMeasLine_Click(object sender, EventArgs e)
        {
            this.objShape = null;
            if (this.isDrawShapeWait)
            {
                this.objShape = snsPictureBox1.DrawAdjMeasLineWait(false, 0.1f);
                snsPictureBox1.Refresh();
            }
            else
            {
                snsPictureBox1.DrawShapeCancel();
                snsPictureBox1.DrawAdjMeasLine(false, 0.1f);
            }
        }

        private void snsPictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (this.objShape is Rectangle)
            {
                Rectangle rect = (Rectangle)objShape;
                if (rect.Width > 0)
                {
                    snsPictureBox1.ShowRect(rect, false);
                }
            }
            else if (this.objShape is PointF)
            {
                PointF point = (PointF)objShape;
                if (point.X > 0 || point.Y > 0)
                {
                    snsPictureBox1.ShowPoint(point, 5);
                }
            }
            else if (this.objShape is SNS.SnsSharp.Line)
            {
                SNS.SnsSharp.Line line = (SNS.SnsSharp.Line)objShape;
                if (line.Width > 0 || line.Height > 0)
                {
                    snsPictureBox1.ShowLine(line.Pt1, line.Pt2, false);
                }
            }
            else if (this.objShape is SNS.SnsSharp.Circle)
            {
                SNS.SnsSharp.Circle showCircle = (SNS.SnsSharp.Circle)objShape;
                if (showCircle.R > 0)
                {
                    snsPictureBox1.ShowCircle(showCircle.Pt, (int)showCircle.R, false);
                }
            }
            else if (this.objShape is SNS.SnsSharp.Polygon)
            {
                SNS.SnsSharp.Polygon showPolygon = (SNS.SnsSharp.Polygon)objShape;
                if (showPolygon.Width > 0 || showPolygon.Height > 0)
                {
                    snsPictureBox1.ShowPolygon(showPolygon.GetListPt(), false);
                }
            }
            else if (this.objShape is SNS.SnsSharp.Polyline)
            {
                SNS.SnsSharp.Polyline showPolyline = (SNS.SnsSharp.Polyline)objShape;
                if (showPolyline.Width > 0 || showPolyline.Height > 0)
                {
                    snsPictureBox1.ShowPolyline(showPolyline.GetListPt(), false);
                }
            }
            else if (this.objShape is FillRectangle)
            {
                FillRectangle rect = (FillRectangle)objShape;
                if (rect.Width > 0)
                {
                    snsPictureBox1.ShowFillRect(rect.TranToRectangleF(), false);
                }
            }
            else if (this.objShape is SNS.SnsSharp.FillCircle)
            {
                SNS.SnsSharp.FillCircle showCircle = (SNS.SnsSharp.FillCircle)objShape;
                if (showCircle.R > 0)
                {
                    snsPictureBox1.ShowFillCircle(showCircle.Pt, (int)showCircle.R, false);
                }
            }
            else if (this.objShape is SNS.SnsSharp.FillPolygon)
            {
                SNS.SnsSharp.FillPolygon showPolygon = (SNS.SnsSharp.FillPolygon)objShape;
                if (showPolygon.Width > 0 || showPolygon.Height > 0)
                {
                    snsPictureBox1.ShowFillPolygon(showPolygon.GetListPt(), false);
                }
            }
            else if (this.objShape is SNS.SnsSharp.MeasLine)
            {
                SNS.SnsSharp.MeasLine showPolygon = (SNS.SnsSharp.MeasLine)objShape;
                if (showPolygon.Width > 0 || showPolygon.Height > 0)
                {
                    snsPictureBox1.ShowMeasLine(showPolygon.Pt1, showPolygon.Pt2, showPolygon.MeasResult, false);
                }
            }
        }
    }
}
